/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ITASwebservice;

import Entities.Userlist;
import Entities.UserlistFacade;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

/**
 *
 * @author Sarah
 */
@Stateless
@Path("entities.userlist")
public class UserlistFacadeREST extends AbstractFacade<Userlist> {
    @EJB
    private UserlistFacade userlistFacade;
    @PersistenceContext(unitName = "IServer-warPU")
    private EntityManager em;

    public UserlistFacadeREST() {
        super(Userlist.class);
    }

    @POST
    @Override
    @Consumes({"application/xml", "application/json"})
    public void create(Userlist entity) {
        super.create(entity);
    }

    @PUT
    @Override
    @Consumes({"application/xml", "application/json"})
    public void edit(Userlist entity) {
        super.edit(entity);
    }
    @POST
    @Path("login")
    @Produces("text/plain")
    @Consumes("application/x-www-form-urlencoded")
    public String login(@FormParam("ID") String id, @FormParam("PWD") String pwd){
        String result="NO";
        result=userlistFacade.login(id, pwd);
        return result;
    }

    @POST
    @Path("join")
    @Produces("text/plain")
    @Consumes("application/x-www-form-urlencoded")
    public String join(@FormParam("ID") String id, @FormParam("PWD") String pwd, @FormParam("NAME") String name){
        String result="NO";
        result=userlistFacade.join(id, pwd, name);
        return result;
    }
    
    @POST
    @Path("cash")
    @Consumes("application/x-www-form-urlenceded")
    public void cash(@FormParam("ID") String id, @FormParam("CASH") String cash) {
        int CASH = Integer.parseInt(cash);
        userlistFacade.addCash(id, CASH);
    }
    
    @POST
    @Path("getCash")
    @Produces("text/plain")
    @Consumes("application/x-www-form-urlenceded")
    public int cash(@FormParam("ID") String id) {
        for (Userlist u : findAll()) {
            if (u.getuId().equals(id))
                return u.getCash();
        }
        return -1;
    }

    @DELETE
    @Path("{id}")
    public void remove(@PathParam("id") Long id) {
        super.remove(super.find(id));
    }

    @GET
    @Path("{id}")
    @Produces({"application/xml", "application/json"})
    public Userlist find(@PathParam("id") Long id) {
        return super.find(id);
    }

    @GET
    @Override
    @Produces({"application/xml", "application/json"})
    public List<Userlist> findAll() {
        return super.findAll();
    }

    @GET
    @Path("{from}/{to}")
    @Produces({"application/xml", "application/json"})
    public List<Userlist> findRange(@PathParam("from") Integer from, @PathParam("to") Integer to) {
        return super.findRange(new int[]{from, to});
    }

    @GET
    @Path("count")
    @Produces("text/plain")
    public String countREST() {
        return String.valueOf(super.count());
    }

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

}
